﻿using System;
using System.Collections.Generic;

namespace Gmantis.Controls
{
    public class Ranking
    {
        // Fields
        private List<int> indexFromRank = new List<int>();
        private List<int> rankFromIndex = new List<int>();

        // Methods
        private void CheckInvariant()
        {
            int num = 0;
            for (int i = 0; i < this.rankFromIndex.Count; i++)
            {
                int num3 = this.rankFromIndex[i];
                if (num3 == -1)
                {
                    num++;
                }
                else
                {
                    int local1 = this.indexFromRank[num3];
                }
            }
        }

        public int IndexFromRank(int rank)
        {
            if (this.indexFromRank.Count <= rank)
            {
                return -1;
            }
            return this.indexFromRank[rank];
        }

        public void OnAdded(int index, int count)
        {
            for (int i = 0; i < this.rankFromIndex.Count; i++)
            {
                if (index <= this.rankFromIndex[i])
                {
                    List<int> list;
                    int num3;
                    (list = this.rankFromIndex)[num3 = i] = list[num3] + count;
                }
                if (index <= this.indexFromRank[i])
                {
                    List<int> list2;
                    int num4;
                    (list2 = this.indexFromRank)[num4 = i] = list2[num4] + count;
                }
            }
            int[] collection = new int[count];
            for (int j = 0; j < count; j++)
            {
                collection[j] = j + index;
            }
            this.rankFromIndex.InsertRange(index, collection);
            this.indexFromRank.InsertRange(index, collection);
            this.CheckInvariant();
        }

        public void OnAdded(int index, int count, int total)
        {
            if (total >= (this.Count + count))
            {
                this.OnAdded(index, count);
                this.OnAdded(this.Count, total - this.Count);
                this.CheckInvariant();
            }
        }

        public void OnRemoved(int index, int count)
        {
            for (int i = 0; i < count; i++)
            {
                int num2 = this.rankFromIndex[i + index];
                this.indexFromRank.RemoveAt(num2);
            }
            this.rankFromIndex.RemoveRange(index, count);
            for (int j = 0; j < this.rankFromIndex.Count; j++)
            {
                if (index <= this.rankFromIndex[j])
                {
                    List<int> list;
                    int num4;
                    (list = this.rankFromIndex)[num4 = j] = list[num4] - count;
                }
                if (index <= this.indexFromRank[j])
                {
                    List<int> list2;
                    int num5;
                    (list2 = this.indexFromRank)[num5 = j] = list2[num5] - count;
                }
            }
            this.CheckInvariant();
        }

        public void OnReset(int count)
        {
            this.rankFromIndex.Clear();
            this.indexFromRank.Clear();
            for (int i = 0; i < count; i++)
            {
                this.rankFromIndex.Add(i);
                this.indexFromRank.Add(i);
            }
            this.CheckInvariant();
        }

        public int RankFromIndex(int index)
        {
            if ((index >= 0) && (this.rankFromIndex.Count > index))
            {
                return this.rankFromIndex[index];
            }
            return -1;
        }

        public void VirtualMove(int oldIndex, int newIndex)
        {
            int num = this.rankFromIndex[oldIndex];
            int num2 = this.rankFromIndex[newIndex];
            if (num < num2)
            {
                for (int i = num; i < num2; i++)
                {
                    int num4 = this.indexFromRank[i + 1];
                    this.indexFromRank[i] = num4;
                    this.rankFromIndex[num4] = i;
                }
                this.indexFromRank[num2] = oldIndex;
                this.rankFromIndex[oldIndex] = num2;
            }
            else
            {
                for (int j = num; num2 < j; j--)
                {
                    int num6 = this.indexFromRank[j - 1];
                    this.indexFromRank[j] = num6;
                    this.rankFromIndex[num6] = j;
                }
                this.indexFromRank[num2] = oldIndex;
                this.rankFromIndex[oldIndex] = num2;
            }
            this.CheckInvariant();
        }

        public void VirtualRemove(int index)
        {
            int num = this.rankFromIndex[index];
            this.indexFromRank.RemoveAt(num);
            this.rankFromIndex[index] = -1;
            for (int i = 0; i < this.rankFromIndex.Count; i++)
            {
                if (num < this.rankFromIndex[i])
                {
                    List<int> list;
                    int num3;
                    (list = this.rankFromIndex)[num3 = i] = list[num3] - 1;
                }
            }
            this.CheckInvariant();
        }

        // Properties
        public int Count
        {
            get
            {
                return this.rankFromIndex.Count;
            }
        }

        public int RankedCount
        {
            get
            {
                return this.indexFromRank.Count;
            }
        }
    }
}
